package com.efounder.frame.fragment;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.core.content.res.ResourcesCompat;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;

import com.core.xml.StubObject;
import com.efounder.frame.activity.EFTransformFragmentActivity;
import com.efounder.frame.utils.EFAppAccountUtils;
import com.efounder.frame.utils.EFConstants;
import com.efounder.frame.utils.EFDrawableUtils;
import com.efounder.frame.utils.EFFrameUtils;
import com.efounder.frame.utils.EFViewTypeUtils;
import com.efounder.frame.widget.EFPopupMenu;
import com.efounder.frame.xmlparse.EFAppAccountRegistry;
import com.efounder.frame.xmlparse.EFXmlConstants;
import com.efounder.mobilemanager.R;
import com.efounder.util.AppContext;

import java.util.List;

/**
 * Created by XinQing on 2016/8/25.
 * EFAppAccountPagerSlidingTabFragment的菜单管理类
 */
public class EFAppAccountPagerSlidingTabFragmentMenuManager {

    /**弹出菜单**/
    private EFPopupMenu popupMenu;

    /**
     * 根据xml配置，设置右上角menu展示样式和点击事件
     * @param menuImageView
     * @param stubObject
     */
    public void configMenu(final ImageView menuImageView, StubObject stubObject){
        final String menuType = stubObject.getString(EFXmlConstants.ATTR_MENU_TYPE,"");
        final String menuId = stubObject.getString(EFXmlConstants.ATTR_MENU_ID,"");
        //设置配置的图片
        String menuIcon = stubObject.getString(EFXmlConstants.ATTR_MENU_ICON,"");
        Drawable drawable = EFDrawableUtils.getDrawableFromConfigurationFile(EFAppAccountUtils.getAppAccountImagePath() + "/",menuIcon);
        if (drawable == null){
            drawable = ResourcesCompat.getDrawable(menuImageView.getResources(), R.drawable.ef_title_view_add,null);
        }
        menuImageView.setImageDrawable(drawable);
        //添加点击事件
        menuImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clickMenu(menuImageView,menuType,menuId);
            }
        });
    }

    /**
     * 不同的配置对应不同的点击事件
     * @param menuImageView
     * @param menuType
     * @param menuId
     */
    private void clickMenu(ImageView menuImageView, String menuType, String menuId){
        if ("menu".equals(menuType)){
            createMenu(menuImageView,menuId);
        }else if ("weather".equals(menuType)){

        }else if ("menuMessage".equals(menuType)){
            //TODO 跳转到应用号消息界面
            try {
                //intent.putExtra("id", user.getId());
                //intent.putExtra("nickName",user.getNickName());
                //bundle中含有 id 和 nickName
                Bundle bundle = EFFrameUtils.getCurrentActivity().getIntent().getExtras();
                bundle.putInt(EFTransformFragmentActivity.EXTRA_TITLE_LEFT_VISIBILITY,View.VISIBLE);
                bundle.putInt(EFTransformFragmentActivity.EXTRA_TITLE_RIGHT_VISIBILITY,View.INVISIBLE);
                bundle.putString(EFTransformFragmentActivity.EXTRA_TITLE_NAME,bundle.getString("nickName"));

                EFFrameUtils.pushFragment((Class<? extends Fragment>) Class.forName(AppContext.getInstance()
                                .getResources().getString(R.string.PUBLIC_CHAT_FRAGMENT_PACKAGE_NAME))
                        ,bundle);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 点击创建弹出菜单
     * @param
     */
    private void createMenu(View anchorView, String menuId){
        if (popupMenu == null){//点击时创建popupMenu
            List<StubObject> menuStubList = EFAppAccountRegistry.getStubListByElementName(menuId);
            popupMenu = new EFPopupMenu(anchorView.getContext());
            for (StubObject stub:menuStubList) {
                EFPopupMenu.PopupMenuItemModel itemModel = popupMenu.new PopupMenuItemModel();
                //1.图片路径
                String normalIconName = stub.getString(EFXmlConstants.ATTR_NORMAL_ICON, "");
                String normalIconPath = EFConstants.IMAGE_DIR + "/" + normalIconName;
                itemModel.setImagePath(normalIconPath);
                itemModel.setText(stub.getCaption());
                popupMenu.addItem(itemModel);
            }
            popupMenu.show(anchorView);
            popupMenu.setOnItemClickListener(new OnMenuItemClickListener(menuStubList));
        }else if (popupMenu.isShowing()){
            popupMenu.dismiss();
        }else {
            popupMenu.show(anchorView);
        }
    }

    /***
     * MenuItem点击监听
     */
    private class OnMenuItemClickListener implements AdapterView.OnItemClickListener{

        private List<StubObject> menuStubList;

        public OnMenuItemClickListener(List<StubObject> menuStubList) {
            this.menuStubList = menuStubList;
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //TODO 点击menu，跳转Activity
            popupMenu.dismiss();
            StubObject menuStub = menuStubList.get(position);
            String viewType = menuStub.getString(EFXmlConstants.ATTR_VIEW_TYPE,"");
            if (viewType.trim().equals("")){
                Log.e("--","view_type.xml中viewType没有配置，或配置为空");
            }else {
                Class activityOrFragmentClass = EFViewTypeUtils.getActivityOrFragmentClass(viewType);
                if (activityOrFragmentClass != null){
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("menuStub",menuStub);
                    EFFrameUtils.push(activityOrFragmentClass,bundle);
                }
            }

        }
    }

}
